/**
 * JAVACC DEMO 1.0
 */
package com.apache.rpc.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.rpc.entity.RpcDatasource;
import com.apache.rpc.manager.RpcDatasourceManager;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.StrUtil;

/**
 * description:  系统数据源管理action
 *
 * @author Administrator 创建时间：2016-11-30
 */
public class DatasourceActionPluginImpl implements PluginConnector {

    private RpcDatasourceManager rpcDatasourceManager;

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    public Object execute(ParamsVo param) throws Exception {
        String method = String.valueOf(param.getParams("method"));
        ResultEntity entity = new ResultEntity();
        entity.setResult("true");
        if ("loadCache".equalsIgnoreCase(method)) {
            loadCache(entity, param);
        } else if ("list".equalsIgnoreCase(method)) {
            list(entity, param);
        } else if ("save".equalsIgnoreCase(method)) {
            save(entity, param);
        } else if ("info".equalsIgnoreCase(method)) {
            info(entity, param);
        }
        param = null;
        return entity;
    }

    private void info(ResultEntity entity, ParamsVo param) {
        String registerId = String.valueOf(param.getParams("sysEname"));
        ParamsVo vo = new ParamsVo();
        vo.setInfoId(registerId);
        Object obj = rpcDatasourceManager.getInfoById(vo);
        entity.setEntity(obj);
        entity.setMessage("查询成功");
    }

    private void loadCache(ResultEntity entity, ParamsVo param) {
        ParamsVo<RpcDatasource> vo = new ParamsVo<RpcDatasource>();
        vo.setMethodKey("initCache");
        rpcDatasourceManager.execute(vo);
        entity.setEntity("true");
        entity.setMessage("操作成功！");
    }

    private void list(ResultEntity entity, ParamsVo param) {
        ParamsVo<RpcDatasource> vo = new ParamsVo<RpcDatasource>();
        String sysEname = String.valueOf(param.getParams("sysEname"));
        String dataBaseType = String.valueOf(param.getParams("dataBaseType"));
        RpcDatasource rds = new RpcDatasource();
        if (Validator.isNotNull(sysEname)) {
            rds.setSysEname(sysEname);
        }
        if (Validator.isNotNull(dataBaseType)) {
            rds.setDatabaseType(dataBaseType);
        }
        vo.setObj(rds);
        String rows = StrUtil.doNull(String.valueOf(param.getParams("pageSize")),
                String.valueOf(param.getParams("rows")));
        String page = StrUtil.doNull(String.valueOf(param.getParams("pageIndex")),
                String.valueOf(param.getParams("page")));
        vo.setParams("pageIndex", page);
        vo.setParams("pageSize", rows);
        Page pageList = rpcDatasourceManager.getPageInfo(vo);
        entity.setEntity(pageList);
        entity.setMessage("查询成功");
    }

    private void save(ResultEntity entity, ParamsVo vo) {
        ParamsVo<RpcDatasource> param = new ParamsVo<RpcDatasource>();
        RpcDatasource item = new RpcDatasource();
        item = (RpcDatasource) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), item);
        if (Validator.isNull(item.getSysEname())) {
            entity.setMessage("数据源名称不能空");
            return;
        } else {
            param.setObj(item);
            param.setInfoId(item.getSysEname());
            Object obj = rpcDatasourceManager.getInfoById(param);
            if (null == obj) {
                String id = rpcDatasourceManager.saveInfo(param);
                if (Validator.isNull(id)) {
                    entity.setMessage("保存失败");
                } else {
                    entity.setEntity(id);
                    entity.setMessage("保存成功");
                }
            } else {
                boolean mark = rpcDatasourceManager.editInfo(param);
                if (mark) {
                    entity.setEntity(mark);
                    entity.setMessage("修改成功");
                } else {
                    entity.setMessage("修改失败");
                }
            }
        }
    }

    public void setRpcDatasourceManager(RpcDatasourceManager rpcDatasourceManager) {
        this.rpcDatasourceManager = rpcDatasourceManager;
    }
}
